package cn.pku.sirx.snad.uias.analyzer.similarity;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import cn.pku.sirx.snad.uias.model.entity.SiteUser;
import cn.pku.sirx.snad.uias.util.AnalyzerUtils;

/**
 * @author WangXiao
 */
public class ClassAnalyzer implements ISimilarityAnalyzer {

	@Override
	public int analyze(SiteUser fromUser, SiteUser toUser) {
		Assert.notNull(fromUser);
		Assert.notNull(toUser);

		double[] valueVector = {
				analyze(fromUser.getClassesJoined(), toUser.getClassesJoined()),
				analyze(fromUser.getClassesAttended(), toUser
						.getClassesAttended()) };
		return (int) AnalyzerUtils.analyzeByWeight(weightVector, valueVector);
	}

	@SuppressWarnings("unchecked")
	private int analyze(String fromClasses, String toClasses) {
		if (fromClasses == null || toClasses == null) {
			return 0;
		}
		List<String> fromClassList = Arrays.asList(StringUtils.split(
				fromClasses, CLASS_SEPARATORS));
		List<String> toClassList = Arrays.asList(StringUtils.split(toClasses,
				CLASS_SEPARATORS));
		return AnalyzerUtils.getCommonCount(fromClassList, toClassList,
				ComparatorUtils.NATURAL_COMPARATOR);
	}

	private final String CLASS_SEPARATORS = ", ;，；";
	private double[] weightVector = { 1, 0.3 };

}
